package com.zr.userrelation.service.impl;

import com.zr.useredu.pojo.UserEdu;
import com.zr.useredu.pojo.UserEduAddVo;
import com.zr.userrelation.mapper.UserRelationMapper;
import com.zr.userrelation.pojo.UserRelation;
import com.zr.userrelation.pojo.UserRelationAddVo;
import com.zr.userrelation.service.UserRelationService;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by Administrator on 2019/7/3.
 */
@Service
public class UserRelationServiceImpl implements UserRelationService {
    @Autowired
    private UserRelationMapper userRelationMapper;

    @Override
    public ResultVo addContacts(UserRelationAddVo userRelationAddVo,HttpServletRequest request) {
        Integer loginId = (Integer) request.getSession().getAttribute("loginId");
        if(loginId == null){
            return ResultBuildVo.error("请登录","500");
        }else{
            UserRelationAddVo userRelationVo = userRelationMapper.queryLoginId(loginId);
            if( userRelationVo != null ){
                UserRelation userRelation = new UserRelation();
                Date nowDate = new Date();
                userRelation.setUpdateTime(nowDate);
                userRelation.setUpdateName("张三");
                //把左边的数据赋值给右边的数据，前提：必须具有相同的参数名称和类型
                BeanUtils.copyProperties(userRelationAddVo,userRelation);
                userRelationMapper.updateContacts(userRelation);
                return ResultBuildVo.success(userRelationAddVo);
            }else{
                UserRelation userRelation = new UserRelation();
                Date nowDate = new Date();
                userRelation.setUpdateTime(nowDate);
                userRelation.setUpdateName("张三");
                //把左边的数据赋值给右边的数据，前提：必须具有相同的参数名称和类型
                BeanUtils.copyProperties(userRelationAddVo,userRelation);
                userRelationMapper.addContacts(userRelation);
                return ResultBuildVo.success(userRelationAddVo);
            }
        }



        //给创建人、时间等赋值
        /*Integer loginId = (Integer) request.getSession().getAttribute("loginId");
        if(loginId == null){
            return ResultBuildVo.error("请登录","500");
        }
        UserRelation userRelation = new UserRelation();
        Date nowDate = new Date();
        userRelation.setCreateTime(nowDate);
        userRelation.setCreateName("张三");
        userRelation.setUpdateTime(nowDate);
        userRelation.setUpdateName("张三");
        //把左边的数据赋值给右边的数据，前提：必须具有相同的参数名称和类型
        BeanUtils.copyProperties(userRelationAddVo,userRelation);
        userRelationMapper.addContacts(userRelation);
        return ResultBuildVo.success(userRelationAddVo);*/
    }

    @Override
    public ResultVo queryLoginId(@RequestParam("loginId") Integer loginId) {
        if (loginId == null){
            return ResultBuildVo.error("没有登录请重新登录","500");
        }
        userRelationMapper.queryLoginId(loginId);
        return ResultBuildVo.success(loginId);
    }
}
